import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

// Vuex.Store 构造器选项
const store = new Vuex.Store({
    //存放状态
    state: {
        // tabbar 数据
        tabbar: {
            backgroundColor: "#ffffff",
            tabBarHeight: 128,
            list: [{
                    pagePath: "/pages/home/home",
                    iconPath: "/static/backlog2.png",
                    selectedIconPath: "/static/backlog.png",
                    text: "",
                },
                {
                    pagePath: "/pages/calendar/calendar",
                    iconPath: "/static/daily2.png",
                    selectedIconPath: "/static/daily.png",
                    text: "",
                },
                {
                    pagePath: "/pages/addtask/addtask?taskState=true",
                    iconPath: "/static/add.png",
                    selectedIconPath: "/static/add.png",
                    isSpecial: true,
                    text: "",
                },
                {
                    pagePath: "/pages/statistical/statistical",
                    iconPath: "/static/statistical2.png",
                    selectedIconPath: "/static/statistical.png",
                    text: "",
                },
                {
                    pagePath: "/pages/user/user",
                    iconPath: "/static/my2.png",
                    selectedIconPath: "/static/my.png",
                    text: "",
                },
            ],
        },
        // 用户信息
        userInfo: {
            nick_name: "微信用户",
            avater: "/static/Head-portrait.png",
        },
        openId: "",
        // 任务分类数据
        taskTypeList: [],
        // 是否需要重新获取首页数据
        isGetDataAgain: false,
    },
    // 变更状态
    mutations: {
        // 修改 tabbar 数据
        editTabbar(state) {
            // 获取数据
            var tabbar = state.tabbar;
            // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面
            var currentPages = getCurrentPages();
            // 获取当前页面元素
            var that = currentPages[currentPages.length - 1];
            // 获取当前页面路径
            var pagePath = that.route;
            // 给 pagePath 最前面添加 /
            pagePath.indexOf("/") != 0 && (pagePath = "/" + pagePath);
            for (var i in tabbar.list) {
                // 给所有 tabbar 中添加一个 selected 的属性，默认false
                tabbar.list[i].selected = false;
                // 判断页面是否是当前页面，并把 selected 属性设置为true
                tabbar.list[i].pagePath == pagePath && (tabbar.list[i].selected = true);
            }
            state.tabbar = tabbar;
        },
        // tabbar 改变事件
        tabbarChange(state, pagePath) {
            var tabbar = state.tabbar;
            tabbar.list.forEach(item => {
                item.pagePath == pagePath ?
                    (item.selected = true) :
                    (item.selected = false);
            });
            state.tabbar = tabbar;
        },
        // 修改 用户信息
        editUserInfo(state, params) {
            state.userInfo = params;
        },
        // 修改 openId
        editOpenId(state, params) {
            state.openId = params;
        },
        // 修改 任务分类数据
        editTaskTypeList(state, params) {
            state.taskTypeList = params;
        },
        // 修改 是否需要重新获取首页数据
        editIsGetDataAgain(state, isAgain) {
            state.isGetDataAgain = isAgain;
        },
    },
    // 异步实现
    actions: {},
});

export default store;